﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YJKRoomInspection.Utiltiy;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitKernel.FormUtility.ModelessFormControlSuite.ModelLayer;
using YJKInspectModelFunc;
using YJKInspectModelFunc.ControlLayer;
using YJKInspectModelFunc.ModelLayer;

namespace YJKRoomInspection.ShowDataForm
{
    public partial class FireProofDirectionForm : System.Windows.Forms.Form, YJKRevitKernel.FormUtility.ModelessFormControlSuite.ModelLayer.IModelessFormNoneSwitch
	{
		public List<BaseDoorPacker> m_lstWrongDoor
		{
			get
			{
				return this.lstWrongDoor;
			}
		}

		public IWin32Window RevitHandle
		{
			get
			{
				return this.m_revitHandle;
			}
			private set
			{
				this.m_revitHandle = value;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public FireProofDirectionForm(IWin32Window inputHandle, List<BaseAreaPacker> input, ExternalCommandData cmdData)
		{
			this.m_uidoc = cmdData.Application.ActiveUIDocument;
			this.m_doc = this.m_uidoc.Document;
			this.InitializeComponent();
			this.m_revitHandle = inputHandle;
			this.InitTreeNode(input);
			foreach (Element element in new FilteredElementCollector(this.m_doc).OfClass(typeof(ViewPlan)).WhereElementIsNotElementType())
			{
				ViewPlan item = element as ViewPlan;
				this.viewList.Add(item);
			}
		}

		private void FireProofDirectionForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				FireProofDirectionForm.SendMessage(SelectFloorForm._hWndSelectFloorForm.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FireProofDirectionForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			if (!string.IsNullOrEmpty(this.strFormCaption))
			{
				this.Text = this.strFormCaption;
			}
		}

		private void InitTreeNode(List<BaseAreaPacker> m_lstAreaPackers)
		{
			new List<Dictionary<int, List<FamilyInstance>>>();
			List<Dictionary<int, List<FamilyInstance>>> levelsFromDoor = this.GetLevelsFromDoor(m_lstAreaPackers);
			this.FireProofDoorDirection.Nodes.Clear();
			int num = 0;
			string text = null;
			string key = null;
			foreach (Dictionary<int, List<FamilyInstance>> dictionary in levelsFromDoor)
			{
				foreach (KeyValuePair<int, List<FamilyInstance>> keyValuePair in dictionary)
				{
					ElementId elementId = new ElementId(keyValuePair.Key);
					Level level = this.m_doc.GetElement(elementId) as Level;
					string name = level.Name;
					string key2 = level.Id.IntegerValue.ToString();
					List<FamilyInstance>.Enumerator enumerator3 = keyValuePair.Value.GetEnumerator();
					this.FireProofDoorDirection.Nodes.Add(key2, name);
					while (enumerator3.MoveNext())
					{
						if (enumerator3.Current != null)
						{
							FamilyInstance familyInstance = enumerator3.Current;
							text = familyInstance.Name;
							key = familyInstance.Id.IntegerValue.ToString();
						}
						this.FireProofDoorDirection.Nodes[num].Nodes.Add(key, text);
					}
					num++;
				}
			}
			this.FireProofDoorDirection.ExpandAll();
			this.FireProofDoorDirection.Sort();
		}

		public List<Dictionary<int, List<FamilyInstance>>> GetLevelsFromDoor(List<BaseAreaPacker> m_lstAreaPackers)
		{
			bool flag = false;
			new List<Level>();
			Dictionary<BaseDoorPacker, Room> dictionary = new Dictionary<BaseDoorPacker, Room>();
			Dictionary<BaseDoorPacker, Room> dictionary2 = new Dictionary<BaseDoorPacker, Room>();
			Dictionary<int, List<FamilyInstance>> dictionary3 = new Dictionary<int, List<FamilyInstance>>();
			List<Dictionary<int, List<FamilyInstance>>> list = new List<Dictionary<int, List<FamilyInstance>>>();
			Dictionary<int, Level> m_Level_index = new Dictionary<int, Level>();
			foreach (BaseAreaPacker tmpAreaPacker in m_lstAreaPackers)
			{
				int num = 0;
				dictionary = YJKRoomInspection.Utiltiy.Utility.GetOutDoorNum(tmpAreaPacker);
				dictionary2 = YJKRoomInspection.Utiltiy.Utility.GetFrontRoomAndStairDoor(tmpAreaPacker);
				using (Dictionary<BaseDoorPacker, Room>.Enumerator enumerator2 = dictionary.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						KeyValuePair<BaseDoorPacker, Room> kvp = enumerator2.Current;
						bool flag2 = YJKInspectModelFunc.Utility.IfDoorToRoom(kvp.Key, kvp.Value);
						if (flag2 && this.lstWrongDoor.Find((BaseDoorPacker n) => n.ThisDoor.Id == kvp.Key.ThisDoor.Id) == null)
						{
							this.lstWrongDoor.Add(kvp.Key);
						}
						num++;
					}
				}
				if (num <= 1)
				{
					flag = true;
				}
				using (Dictionary<BaseDoorPacker, Room>.Enumerator enumerator2 = dictionary2.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						KeyValuePair<BaseDoorPacker, Room> kvp = enumerator2.Current;
						bool flag2 = YJKInspectModelFunc.Utility.IfDoorToRoom(kvp.Key, kvp.Value);
						if (flag != flag2 && this.lstWrongDoor.Find((BaseDoorPacker n) => n.ThisDoor.Id == kvp.Key.ThisDoor.Id) == null)
						{
							this.lstWrongDoor.Add(kvp.Key);
						}
					}
				}
			}
			using (List<BaseDoorPacker>.Enumerator enumerator3 = this.lstWrongDoor.GetEnumerator())
			{
				while (enumerator3.MoveNext())
				{
					BaseDoorPacker tmpPacker = enumerator3.Current;
					if (tmpPacker.ThisDoor.CanFlipFacing)
					{
						Parameter parameter = tmpPacker.ThisDoor.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
						if (parameter != null)
						{
							Level level = this.m_doc.GetElement(parameter.AsElementId()) as Level;
							if (!dictionary3.Keys.Contains(level.Id.IntegerValue))
							{
								dictionary3.Add(level.Id.IntegerValue, new List<FamilyInstance>());
								m_Level_index.Add(level.Id.IntegerValue, level);
							}
							if (dictionary3[level.Id.IntegerValue].Find((FamilyInstance n) => n.Id == tmpPacker.ThisDoor.Id) == null)
							{
								dictionary3[level.Id.IntegerValue].Add(tmpPacker.ThisDoor);
							}
						}
					}
				}
			}
			dictionary3 = (from k in dictionary3
			orderby m_Level_index[k.Key].Elevation
			select k).ToDictionary((KeyValuePair<int, List<FamilyInstance>> k) => k.Key, (KeyValuePair<int, List<FamilyInstance>> k) => k.Value);
			list.Add(dictionary3);
			return list;
		}

		private void FireProofDoorDirection_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
		{
			this.m_strOrderType = "doubleClick";
			this.m_bIfContinue = true;
			this.m_bIfBreak = false;
			this.m_bIfManuEsc = false;
			this.SendEscMessageToRevitAndClearSelect();
			this.m_useBeforeCotinueMethod = new FireProofDirectionForm.BeforeContinueMethod(this.LocationMethod);
		}

		private void LocationMethod()
		{
			string name = this.FireProofDoorDirection.SelectedNode.Name;
			char[] separator = new char[]
			{
				','
			};
			string[] array = name.Split(separator);
			Selection selection = this.m_uidoc.Selection;
			List<Element> list = new List<Element>();
			ElementSet elementSet = new ElementSet();
			ElementId elementId = null;
			int num = 0;
			while ((long)num < array.LongCount<string>())
			{
				ElementId elementId2 = new ElementId(int.Parse(array[num]));
				Element element = this.m_doc.GetElement(elementId2);
				if (element != null)
				{
					if (element is Level)
					{
						elementId = element.Id;
						for (int i = 0; i < this.lstWrongDoor.Count; i++)
						{
							Parameter parameter = this.lstWrongDoor[i].ThisDoor.get_Parameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
							if (parameter != null && (this.m_doc.GetElement(parameter.AsElementId()) as Level).Name == element.Name)
							{
								list.Add(this.lstWrongDoor[i].ThisDoor);
								elementSet.Insert(this.lstWrongDoor[i].ThisDoor);
							}
						}
					}
					else if (element is FamilyInstance)
					{
						elementId = element.LevelId;
						list.Add(element);
						elementSet.Insert(element);
					}
				}
				num++;
			}
			foreach (ViewPlan viewPlan in this.viewList)
			{
				if (!(elementId == null))
				{
					int integerValue = elementId.IntegerValue;
					if (viewPlan.GenLevel != null && viewPlan.GenLevel.Id.IntegerValue == integerValue)
					{
						this.m_uidoc.ActiveView = viewPlan;
						break;
					}
				}
			}
			if (elementSet.Size != 0)
			{
				this.m_uidoc.ShowElements(elementSet);
				RevitVersionFuncs.SetSelection(selection, list);
				this.m_uidoc.RefreshActiveView();
			}
		}

		private void SelectAllBtn_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.FireProofDoorDirection.Nodes.Count; i++)
			{
				for (int j = 0; j < this.FireProofDoorDirection.Nodes[i].Nodes.Count; j++)
				{
					this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked = true;
				}
			}
		}

		private void NoSelectAllBtn_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.FireProofDoorDirection.Nodes.Count; i++)
			{
				for (int j = 0; j < this.FireProofDoorDirection.Nodes[i].Nodes.Count; j++)
				{
					this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked = false;
				}
			}
		}

		private void ReverseSelectBtn_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.FireProofDoorDirection.Nodes.Count; i++)
			{
				for (int j = 0; j < this.FireProofDoorDirection.Nodes[i].Nodes.Count; j++)
				{
					if (!this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked)
					{
						this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked = true;
					}
					else
					{
						this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked = false;
					}
				}
			}
		}

		private List<Element> GetSelNodes()
		{
			List<Element> list = new List<Element>();
			List<TreeNode> list2 = new List<TreeNode>();
			for (int i = 0; i < this.FireProofDoorDirection.Nodes.Count; i++)
			{
				if (this.FireProofDoorDirection.Nodes[i].Checked)
				{
					list2.AddRange(TreeViewTools.GetAllChildNode(this.FireProofDoorDirection.Nodes[i]));
				}
				else
				{
					for (int j = 0; j < this.FireProofDoorDirection.Nodes[i].Nodes.Count; j++)
					{
						if (this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked)
						{
							list2.Add(this.FireProofDoorDirection.Nodes[i].Nodes[j]);
						}
					}
				}
			}
			foreach (TreeNode treeNode in list2)
			{
				string[] array = treeNode.Name.Split(new char[]
				{
					','
				});
				Element element = this.m_doc.GetElement(new ElementId(int.Parse(array[0])));
				if (element != null && element is FamilyInstance)
				{
					list.Add(element);
				}
			}
			return list;
		}

		private void GetNeedFlipDoors()
		{
			List<ElementId> list = new List<ElementId>();
			foreach (FamilyInstance familyInstance in this.lstSelfi)
			{
				list.Add(familyInstance.Id);
			}
			foreach (BaseDoorPacker baseDoorPacker in this.lstWrongDoor)
			{
				if (list.Contains(baseDoorPacker.ThisDoor.Id))
				{
					this.m_lstSelFlipDoors.Add(baseDoorPacker);
				}
			}
		}

		private void ModifyBtn_Click(object sender, EventArgs e)
		{
			this.m_strOrderType = "ModifyDirection";
			this.GetModifyDoor();
			this.m_bIfContinue = true;
			this.m_bIfBreak = false;
			this.m_bIfManuEsc = false;
			this.SendEscMessageToRevitAndClearSelect();
			this.m_useBeforeCotinueMethod = new FireProofDirectionForm.BeforeContinueMethod(this.LocationMethod);
		}

		private void GetModifyDoor()
		{
			new List<Element>();
			foreach (Element element in this.GetSelNodes())
			{
				this.lstSelfi.Add(element as FamilyInstance);
			}
			for (int i = 0; i < this.FireProofDoorDirection.Nodes.Count; i++)
			{
				if (this.FireProofDoorDirection.Nodes[i].Checked)
				{
					this.FireProofDoorDirection.Nodes[i].Remove();
					i--;
				}
				else
				{
					for (int j = 0; j < this.FireProofDoorDirection.Nodes[i].Nodes.Count; j++)
					{
						if (this.FireProofDoorDirection.Nodes[i].Nodes[j].Checked)
						{
							this.FireProofDoorDirection.Nodes[i].Nodes[j].Remove();
						}
					}
				}
			}
			this.GetNeedFlipDoors();
		}

		private void CloseBtn_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void FireProofDirectionForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			List<int> notDeleteRoom = YJKRoomInspection.Utiltiy.Utility.GetNotDeleteRoom(this.m_doc);
			ElementPackerManger.TryDeletedAddedRoom(this.m_doc, notDeleteRoom);
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		public bool IfBreak
		{
			get
			{
				return this.m_bIfBreak;
			}
		}

		public bool IfContinue
		{
			get
			{
				return this.m_bIfContinue;
			}
		}

		public bool IfManualEsc
		{
			get
			{
				return this.m_bIfManuEsc;
			}
		}

		public void CloseForm()
		{
			if (!base.IsDisposed)
			{
				base.Close();
			}
		}

		public void ResetContinueBreak()
		{
			this.m_bIfContinue = false;
			this.m_bIfBreak = true;
			this.m_bIfManuEsc = true;
		}

		public void SendEscMessageToRevitAndClearSelect()
		{
			this.m_useBeforeCotinueMethod = null;
			RevitVersionFuncs.ClearSelection(this.m_uidoc.Selection);
			YJKInspectModelFunc.Utility.SendEscToHandle(this.RevitHandle);
		}

		public void ShowForm(IWin32Window parentForm = null)
		{
			if (this.RevitHandle != null)
			{
				base.Show(this.RevitHandle);
				return;
			}
			base.Show();
		}

		public void DoBeforeContinue()
		{
			if (this.m_useBeforeCotinueMethod != null)
			{
				this.m_useBeforeCotinueMethod();
			}
		}

		public void ModifyDoorDirection()
		{
			YJKRoomInspection.Utiltiy.Utility.FlipDoor(this.m_doc, this.m_lstSelFlipDoors);
		}

		private Document m_doc;

		public UIDocument m_uidoc;

		public string strFormCaption;

		private List<ViewPlan> viewList = new List<ViewPlan>();

		private List<BaseDoorPacker> lstWrongDoor = new List<BaseDoorPacker>();

		private List<FamilyInstance> lstSelfi = new List<FamilyInstance>();

		private List<BaseDoorPacker> m_lstSelFlipDoors = new List<BaseDoorPacker>();

		private IWin32Window m_revitHandle;

		private FireProofDirectionForm.BeforeContinueMethod m_useBeforeCotinueMethod;

		private List<BaseAreaPacker> m_inputValue = new List<BaseAreaPacker>();

		private ViewPlan m_frontViewPlan;

		public List<Element> useRoom = new List<Element>();

		public List<InsertDoor> lsttmpdata = new List<InsertDoor>();

		public string m_strOrderType = "doubleClick";

		private delegate void BeforeContinueMethod();
	}
}
